import { Confilg } from 'config.js';
class Base {
  constructor() {
    this.baseRestUrl = Confilg.restUrl;
    this.userKey = 'user'
  }

  request(params, noRequest) {
    var url = this.baseRestUrl + params.url;
    if (!params.type) {
      params.type = 'GET'
    }

    wx.request({
      url: url,
      data:params.data,
      header:{
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token')
      },
      method:params.type,
      success:(res) =>{
        var code = res.statusCode.toString();
        if(code == 200){
          if(res.data.code != 200 ){
            params.eCallBack && params.eCallBack(res);
          }else{
            params.sCallBack && params.sCallBack(res.data);
          }
        }else{
          if(res.data.code == 401){
            params.eCallBack && params.eCallBack(res);
          }
        }
      },
      fail:(err) =>{

      }
    })
  }
   /*获得元素上的绑定的值*/
   getDataSet(event, key) {
    return event.currentTarget.dataset[key];
  };
 //检查user是否存在
 checkUser(){
    var user = wx.getStorageSync(this.userKey);
    return user;
 }
 //写入user
 setUserKey(user){
    wx.setStorageSync(this.userKey, user)
 }
 //写入token
 setTokenKey(token){
  wx.setStorageSync('token', token)
 }
}


export { Base };